package com.yy.wga.admin.service.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yy.wga.common.core.aop.DistributedLock;
import com.yy.wga.mp.common.interceptor.RequestUserHolder;
import com.yy.wga.mp.common.utils.mapping.BeansUtils;
import com.yy.wga.mp.module.admin.request.AdminOrderUpgradePageRequest;
import com.yy.wga.mp.module.admin.response.AdminOrderDeliveryAddressSaveRequest;
import com.yy.wga.mp.module.admin.response.AdminOrderDeliveryInfoResponse;
import com.yy.wga.mp.module.admin.response.AdminOrderUpgradePageResponse;
import com.yy.wga.mp.module.order.entity.pojo.OrderDeliveryAddressDO;
import com.yy.wga.mp.module.order.service.OrderDeliveryAddressService;
import com.yy.wga.mp.module.order.service.OrderUpGradeService;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 运营后台/直通车商品订单管理
 *
 * @author guochao
 * @since 2024-11-01 21:14:37
 */
@RestController
@RequestMapping("admin/order/upgrade")
public class AdminOrderUpgradeController {
    /**
     * 服务对象
     */
    @Resource
    private OrderUpGradeService orderUpGradeService;
    @Resource
    private OrderDeliveryAddressService deliveryAddressService;

    /**
     * 分页
     * @param request
     * @return
     */
    @PostMapping("page")
    public Result<PageBean<AdminOrderUpgradePageResponse>> page(@Valid @RequestBody AdminOrderUpgradePageRequest request) {
        PageBean<AdminOrderUpgradePageResponse> pageBean = orderUpGradeService.pageData(request);
        return Result.success(pageBean);
    }

    /**
     * 获取收获信息
     * @param orderNo
     * @return
     */
    @GetMapping("getDelivery")
    public Result<AdminOrderDeliveryInfoResponse> getDelivery(@RequestParam("orderNo") String orderNo) {
        OrderDeliveryAddressDO deliveryAddress = deliveryAddressService.getOne(new LambdaQueryWrapper<OrderDeliveryAddressDO>()
                .eq(OrderDeliveryAddressDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey())
                .eq(OrderDeliveryAddressDO::getOrderNo, orderNo));
        return Result.success(BeansUtils.map(deliveryAddress, AdminOrderDeliveryInfoResponse.class));
    }

    /**
     * 发货
     * @param request
     * @return
     */
    @PostMapping("saveDelivery")
    public Result<Boolean> saveDelivery(@Valid @RequestBody AdminOrderDeliveryAddressSaveRequest request) {
        orderUpGradeService.delivery(request);
        return Result.success(Boolean.TRUE);
    }

    /**
     * 导出
     * @param request
     * @return
     */
    @GetMapping("export")
    @DistributedLock(value = "'exportUpgradeOrder'")
    public void export(@Valid @RequestBody AdminOrderUpgradePageRequest request) {
        orderUpGradeService.export(request);
    }
}
